package crm.control;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Collection;

import crm.dbcoon.DBConnect;
import crm.model.Notice;
import crm.model.Saleskills;
import crm.util.Pagination;

public class SaleskillsControl {
    
	public static Pagination pageInstance = null;
	
	//获取一条动态
    public Saleskills getOneSaleskillsById(int id){
 	   DBConnect dbc = new DBConnect();
 	   String sql = "select * from sale_skill where id = "+id;
 	  Saleskills saleskills = new Saleskills();
 	   dbc.prepareStatement(sql);
 	   try{
 		   ResultSet rs = dbc.executeQuery();
 		   while(rs.next()){
 			  saleskills.setId(id);
 			  saleskills.setTheme(rs.getString("theme"));
 			  saleskills.setContent(rs.getString("content"));
 			  saleskills.setWriter(rs.getInt("writer"));
 			      
 		   }
 	   }
 		   catch(SQLException e){
 			   e.printStackTrace();
 		   }
 		   finally{
 			   dbc.close();
 		   }
 		   return saleskills;
 	   }
    
	// 增加销售技巧
	public boolean insertSaleskills(Saleskills saleskills) {
		DBConnect dbc = new DBConnect();
		String sql = "insert into sale_skill(theme,content,writer)values(?,?,?)";
		dbc.prepareStatement(sql);
		try {
			// dbc.setInt(1,thought.getId());
			dbc.setString(1, saleskills.getTheme());
			dbc.setString(2, saleskills.getContent());
			dbc.setInt(3, saleskills.getWriter());
			dbc.executeUpdate();
			return true;
		} catch (SQLException e) {
			e.printStackTrace();
			return false;
		} finally {
			dbc.close();
		}
	}

	// 根据id删除销售技巧
	public boolean deleteSaleskills(int id) {
		DBConnect dbc = new DBConnect();
		String sql = "delete from sale_skill where id=?";
		dbc.prepareStatement(sql);
		try {
			dbc.setInt(1, id);
			dbc.executeUpdate();
			return true;
		} catch (SQLException e) {
			e.printStackTrace();
			return false;
		} finally {
			dbc.close();
		}
	}

	// 根据id修改销售技巧
	public boolean updateSaleskills(Saleskills saleskills, int id) {
		DBConnect dbc = new DBConnect();
		String sql = "update sale_skill set theme='" + saleskills.getTheme()
				+ "',content='" + saleskills.getContent() + "',writer='" + saleskills.getWriter()
				+  "'where id=" + id;
		dbc.prepareStatement(sql);
		try {
			dbc.executeUpdate();
			return true;
		} catch (SQLException e) {
			e.printStackTrace();
			return false;
		} finally {
			dbc.close();
		}
	}
	
	// 分页显示
	public Collection GetSaleskills(int page, int rowsPerPage, String addtime)
			throws SQLException, Exception {
		String sql = "SELECT * FROM sale_skill order by " + addtime + " desc";
		pageInstance = new SaleskillsControlPage(sql, rowsPerPage);
		return pageInstance.getPage(page);
	}

	class SaleskillsControlPage extends Pagination {
		public SaleskillsControlPage(String sql, int rowsPerPage) {
			super(sql, rowsPerPage, 1);
		}

		protected Collection<Saleskills> packResultSet(ResultSet rs)
				throws SQLException {
			Collection<Saleskills> SaleskillsList = new ArrayList<Saleskills>();
			while (rs.next()) {
				Saleskills s = new Saleskills();
				s.setId(rs.getInt("id"));
				s.setTheme(rs.getString("theme"));
				s.setContent(rs.getString("content"));
				s.setWriter(rs.getInt("writer"));
				SaleskillsList.add(s);
				s = null;
			}
			return SaleskillsList;
		}
	}

}
